package net.hn.hnms.biz.emgcy.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.Data;
import lombok.EqualsAndHashCode;
import net.hn.hnms.biz.emgcy.domain.ExpertPo;
import net.hn.hnms.biz.emgcy.service.ExpertService;
import net.hn.hnms.biz.emgcy.template.ExpertTemplate;
import net.hn.hnms.sys.system.api.RemoteDictService;
import net.hn.hnms.sys.system.api.domain.SysDictData;


import java.util.ArrayList;
import java.util.List;

/**
 * @param * @param null
 * @author baijiajun
 * @date 2022/1/20
 * @return
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class ExpertImportListener extends AnalysisEventListener<ExpertTemplate> {

    private ExpertService expertService;

    private RemoteDictService remoteDictService;

    private List<ExpertTemplate> list = new ArrayList<>();

    @Override
    public void invoke(ExpertTemplate expertTemplate, AnalysisContext context) {
        this.list.add(expertTemplate);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
       ArrayList<ExpertPo> experts = new ArrayList<>();
        list.forEach(s-> { {
                try {
                    ExpertPo expert = new ExpertPo();
                    expert.setAddress(s.getAddress());
                    //expert.setEmail(s.getEmail());
                    expert.setEmResume(s.getEmResume());
                    //expert.setGraduate(s.getGraduate());
                    expert.setName(s.getName());
                    /*SysDictData dict = remoteDictService.queryInfo("qualification_code", s.getQualificationCode());
                    if(null != dict){
                        expert.setQualificationCode(dict.getDictValue());
                    }else{
                        expert.setQualificationCode(null);
                    }*/

                    SysDictData sysDict = remoteDictService.queryInfo("expert_type",s.getType());
                    if(null != sysDict){
                        expert.setType(sysDict.getDictValue());
                    }else{
                        expert.setType(null);
                    }
                    //expert.setProTitle(s.getProTitle());
                    expert.setSpeciality(s.getSpeciality());
                    expert.setTel(s.getTel());
                    expert.setUnit(s.getUnit());
                    experts.add(expert);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        expertService.saveBatch(experts);
        this.list.clear();
    }

    public ExpertImportListener(RemoteDictService remoteDictService,ExpertService expertService) {
        this.remoteDictService = remoteDictService;
        this.expertService=expertService;
    }



}
